package com.palace.saw.module.app.controller.app.configtype;

import com.palace.saw.module.app.controller.admin.configtype.vo.ConfigTypePageReqVO;
import com.palace.saw.module.app.controller.admin.configtype.vo.ConfigTypeRespVO;
import com.palace.saw.module.app.controller.admin.configtype.vo.ConfigTypeSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.palace.saw.framework.common.pojo.PageParam;
import com.palace.saw.framework.common.pojo.PageResult;
import com.palace.saw.framework.common.pojo.CommonResult;
import com.palace.saw.framework.common.util.object.BeanUtils;
import static com.palace.saw.framework.common.pojo.CommonResult.success;

import com.palace.saw.framework.excel.core.util.ExcelUtils;

import com.palace.saw.framework.apilog.core.annotation.ApiAccessLog;
import static com.palace.saw.framework.apilog.core.enums.OperateTypeEnum.*;

import com.palace.saw.module.app.dal.dataobject.configtype.ConfigTypeDO;
import com.palace.saw.module.app.service.configtype.ConfigTypeService;

@Tag(name = "用户 APP - 配置类型")
@RestController
@RequestMapping("/app/config-type")
@Validated
public class AppConfigTypeController {

    @Resource
    private ConfigTypeService configTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建配置类型")
    public CommonResult<Long> createConfigType(@Valid @RequestBody ConfigTypeSaveReqVO createReqVO) {
        return success(configTypeService.createConfigType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配置类型")
    public CommonResult<Boolean> updateConfigType(@Valid @RequestBody ConfigTypeSaveReqVO updateReqVO) {
        configTypeService.updateConfigType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配置类型")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteConfigType(@RequestParam("id") Long id) {
        configTypeService.deleteConfigType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配置类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ConfigTypeRespVO> getConfigType(@RequestParam("id") Long id) {
        ConfigTypeDO configType = configTypeService.getConfigType(id);
        return success(BeanUtils.toBean(configType, ConfigTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配置类型分页")
    public CommonResult<PageResult<ConfigTypeRespVO>> getConfigTypePage(@Valid ConfigTypePageReqVO pageReqVO) {
        PageResult<ConfigTypeDO> pageResult = configTypeService.getConfigTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配置类型 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigTypeExcel(@Valid ConfigTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigTypeDO> list = configTypeService.getConfigTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配置类型.xls", "数据", ConfigTypeRespVO.class,
                        BeanUtils.toBean(list, ConfigTypeRespVO.class));
    }

}